class Solution
{
public:
    int dieSimulator(int n, vector<int> &rollMax)
    {
        const int MOD = 1e9 + 7;
        vector<vector<vector<int>>> f(n + 1, vector<vector<int>>(6, vector<int>(16)));
        for (int i = 0; i < 6; i ++ ) f[1][i][1] = 1;
        for (int i = 1; i < n; i ++ )
            for (int j = 0; j < 6; j ++ )
                for (int k = 1; k <= rollMax[j]; k ++ )
                    for (int u = 0; u < 6; u ++ )
                    {
                        int len = 1;
                        if (u == j)
                        {
                            len = k + 1;
                            if (len > rollMax[j]) continue;
                        }
                        f[i + 1][u][len] = (f[i + 1][u][len] + f[i][j][k]) % MOD;
                    }

        int res = 0;
        for (int i = 0; i < 6; i ++ )
            for (int j = 1; j <= rollMax[i]; j ++ )
                res = (res + f[n][i][j]) % MOD;
        return res;
    }
};
